﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.ComponentModel.DataAnnotations;
using System.Windows;
using Validator.Properties;

namespace Validator.ViewModels
{
    partial class MainViewModel : ObservableValidator
    {
        [ObservableProperty]
        //[Required]
        [Required(ErrorMessageResourceName = "UserNameRequired", ErrorMessageResourceType = typeof(Lang))]
        [MinLength(3, ErrorMessageResourceName = "UserName_MinLength", ErrorMessageResourceType = typeof(Lang))]
        //[MinLength(3)]
        [MaxLength(12)]
        string? name;

        [ObservableProperty]
        [Required(ErrorMessageResourceName = "AddressRequired", ErrorMessageResourceType = typeof(Lang))]
        [EmailAddress(ErrorMessageResourceName = "Address_Format", ErrorMessageResourceType = typeof(Lang))]
        string? email;

        /*private int age;

        public int Age
        {
            get { return age; }
            set
            {
                SetProperty(ref age, value, true);
                //ValidateProperty(value);
            }
        }*/

        [ObservableProperty]
        [Required(ErrorMessageResourceName = "AgeRequired", ErrorMessageResourceType = typeof(Lang))]
        [Range(0, 150, ErrorMessageResourceName = "Age_Range", ErrorMessageResourceType = typeof(Lang))]
        int? age;
        partial void OnAgeChanged(int? value)
        {
            ValidateProperty(value, nameof(Age));
        }

        [ObservableProperty]
        string? userNameErrorMessage;
        [ObservableProperty]
        string? emailErrorMessage;
        [ObservableProperty]
        string? ageErrorMessage;
        [RelayCommand]
        void Register()
        {
            ValidateAllProperties();

            if (HasErrors)
            {
                UserNameErrorMessage = string.Join(Environment.NewLine, GetErrors(nameof(Name)));
                EmailErrorMessage = string.Join(Environment.NewLine, GetErrors(nameof(Email)));
                AgeErrorMessage = string.Join(Environment.NewLine, GetErrors(nameof(Age)));
                return;
            }
            UserNameErrorMessage = "";
            EmailErrorMessage = "";
            AgeErrorMessage = "";
            MessageBox.Show("注册成功");

        }
    }
}
/*MessageBox.Show("注册失败");
MessageBox.Show(string.Join(Environment.NewLine,GetErrors()));
UserNameErrorMessage = string.Join(Environment.NewLine,GetErrors());*/

